#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTextFrame>
#include <QDebug>
#include <QFileDialog>
#include <QImageReader>
#include <QPushButton>


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    //获取文档对象
    QTextDocument *document=ui->textEdit->document();

    //获取跟框架
    QTextFrame *rootFrame=document->rootFrame();

    //文档框架格式
    QTextFrameFormat format;
    format.setBorderBrush(Qt::blue);     //边框颜色
    format.setBorder(4);                 //边框宽度
    //文档框架设置格式
    rootFrame->setFrameFormat(format);

    //设置文本边框风格
    QTextFrameFormat frameFormat;
    frameFormat.setBackground(Qt::lightGray);
    frameFormat.setMargin(10);     //设置文本边框与外面边界的距离
    frameFormat.setPadding(5);    //设置文本与文本边框的距离
    frameFormat.setBorder(2);     //边界宽度
    frameFormat.setBorderStyle(QTextFrameFormat::BorderStyle_Dashed);   //设置边框风格为点样式

    QTextCursor cursor=ui->textEdit->textCursor();   //获取文本光标
    cursor.insertFrame(frameFormat);

    QAction *action_textFrame=new QAction("框架",this);
    connect(action_textFrame,&QAction::triggered,this,&MainWindow::showTextFrame);
    ui->mainToolBar->addAction(action_textFrame);

    QAction *action_textBlock=new QAction("文本块",this);
    connect(action_textBlock,&QAction::triggered,this,&MainWindow::showTextBlock);
    ui->mainToolBar->addAction(action_textBlock);

    QAction *action_textFront=new QAction("字体",this);
    action_textFront->setCheckable(true);      //因为信号传参，所以需要默认按钮为可选
    connect(action_textFront,&QAction::triggered,this,&MainWindow::setTextFront);
    ui->mainToolBar->addAction(action_textFront);

    QAction *action_textTable=new QAction("表格",this);
    QAction *action_textList=new QAction("列表",this);
    QAction *action_textImage=new QAction("图片",this);
    connect(action_textTable,&QAction::triggered,this,&MainWindow::insertTabel);
    connect(action_textList,&QAction::triggered,this,&MainWindow::insertList);
    connect(action_textImage,&QAction::triggered,this,&MainWindow::insertImage);

    ui->mainToolBar->addAction(action_textTable);
    ui->mainToolBar->addAction(action_textList);
    ui->mainToolBar->addAction(action_textImage);

    m_sLighter=new MySyntaxHignlghter(ui->textEdit->document());

    QAction *action_textfind=new QAction("查找",this);
    connect(action_textfind,&QAction::triggered,this,&MainWindow::textFind);
    ui->mainToolBar->addAction(action_textfind);

    m_findDialg=new QDialog(this);        //查找对话框
    m_lineEdit=new QLineEdit(m_findDialg);     //查找输入框
    QPushButton *btn=new QPushButton(m_findDialg);    //查找下一个按钮
    btn->setText("查找下一个");
    connect(btn,&QPushButton::clicked,this,&MainWindow::textNext);

    QVBoxLayout *layout=new QVBoxLayout;
    layout->addWidget(m_lineEdit);
    layout->addWidget(btn);
    m_findDialg->setLayout(layout);



}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::showTextFrame()
{
    //获取文档对象
    QTextDocument *document=ui->textEdit->document();
    //获取根框架
    QTextFrame *frame=document->rootFrame();

    QTextFrame::iterator it;
    for(it=frame->begin();!(it.atEnd());++it)
    {
        //获取当前框架指针
        QTextFrame *childFrame=it.currentFrame();

        //获取当前文本块
        QTextBlock childBlock=it.currentBlock();
        if(childFrame)
            qDebug()<<"frame";
        else if(childBlock.isValid())
            qDebug()<<"block:"<<childBlock.text();

    }



}

void MainWindow::showTextBlock()
{
    QTextDocument *document=ui->textEdit->document();
    QTextBlock block=document->firstBlock();
    for(int i=0;i<document->blockCount();i++)
    {
        qDebug()<<QString("文本块：%1,文本块首行行号为：%2,长度：%3，内容：%4")
                          .arg(i)
                          .arg(block.firstLineNumber())
                          .arg(block.length())
                          .arg(block.text());
        block=block.next();
    }

}

void MainWindow::setTextFront(bool checked)
{
    if(checked)
    {
        QTextCursor cursor=ui->textEdit->textCursor();
        //文本块格式
        QTextBlockFormat blockFormat;
        //居中对齐
        blockFormat.setAlignment(Qt::AlignCenter);
        cursor.insertBlock(blockFormat);

        //字符格式
        QTextCharFormat charFormat;
        charFormat.setBackground(Qt::lightGray);
        //字符颜色
        charFormat.setForeground(Qt::blue);
        charFormat.setFont(QFont(QString("宋体"),12,QFont::Bold,true));
        charFormat.setFontUnderline(true);     //下划线
        cursor.setCharFormat(charFormat);
        //设置字符格式
        cursor.insertText("加油！加油！加油！");
    }

}

void MainWindow::insertTabel()
{
    QTextCursor cursor=ui->textEdit->textCursor();
    QTextTableFormat format;     //表格格式
    format.setCellSpacing(2);     // 表格外边白
    format.setCellPadding(10);    //表格内边白
    cursor.insertTable(3,3,format);

}

void MainWindow::insertList()
{
    QTextListFormat format;    //
    format.setStyle(QTextListFormat::ListDecimal);    //数字编号格式
    ui->textEdit->textCursor().insertList(format);

}

void MainWindow::insertImage()
{
    //打开图片后返回图片路径
//    QString filepath =QFileDialog::getOpenFileName(this,"选择图片",
//                                               ".",
//                                               "JPEG(*.jpg *.jpeg);;"
//                                               "GIF(*.gif);;"
//                                               "PNG(*.png)");
    QString filepath =QFileDialog::getOpenFileName(this,"选择图片",
                                               ".",
                                               "JPEG(*.jpg *.jpeg);;""GIF(*.gif);;" "PNG(*.png)");

    QUrl url(QString("file://%1").arg(filepath));   //链接
    QImage image=QImageReader(filepath).read();   //将读取到的文件保存在image中

    QTextDocument *document=ui->textEdit->document();
    //文档添加图片资源
   document->addResource(QTextDocument::ImageResource,url,QVariant(image));  //图片资源，资源名字，类型

    QTextCursor cursor=ui->textEdit->textCursor();
    QTextImageFormat imgFormat;
    imgFormat.setWidth(image.width());
    imgFormat.setHeight(image.height());
    imgFormat.setName(url.toString());    //路径
    cursor.insertImage(imgFormat);

}

void MainWindow::textFind()
{
    m_findDialg->show();
}

void MainWindow::textNext()
{
    QString strFind =m_lineEdit->text();
    bool isFind=ui->textEdit->find(strFind,QTextDocument::FindBackward);
    if(isFind){
        qDebug()<<QString("行号：%1,列号：%2")
                  .arg(ui->textEdit->textCursor().blockNumber())
                  .arg(ui->textEdit->textCursor().columnNumber());

    }
}


















